package cn.hs.service.impl;

import cn.hs.entity.Company;
import cn.hs.mapper.CompanyMapper;
import cn.hs.pojo.CompanySelect;
import cn.hs.service.CompanyService;
import cn.hs.util.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName CompanyServiceImpl
 * @Description TODO
 * @Author yun
 * @Date 2024年10月23日 21:14
 * @Version 1.0
 */
@Service
public class CompanyServiceImpl implements CompanyService {

    @Autowired
    private CompanyMapper mapper;

    @Override
    public Result add(Company company) {
        if(company == null){
            return Result.error();
        }

        if(mapper.add(company) == 1){
            return Result.success();
        }
        return Result.error();
    }

    @Override
    public Result delete(Integer id) {
        if(id == null){
            return Result.error();
        }
        if(mapper.delete(id) == 1){
            return Result.success();
        }
        return Result.error();
    }

    @Override
    public Result update(Company company) {
        if(company == null){
            return Result.error();
        }
        if(mapper.update(company) == 1){
            return Result.success();
        }
        return Result.error();
    }

    @Override
    public Result selectAll(CompanySelect select) {
        if(select != null){
            if(select.getPageNo() != null && select.getPageSize() != null){
                PageHelper.startPage(select.getPageNo(), select.getPageSize());
            }
        }
        List<Company> list = mapper.selectAll(select);
        PageInfo<Company> info = new PageInfo<>(list);
        return Result.success(info);
    }

    @Override
    public Result selectById(Integer id) {
        if(id == null){
            return Result.error();
        }
        Company company = mapper.selectById(id);
        if(company == null){
            return Result.error("获取数据为空");
        }
        return Result.success(company);
    }
}
